package com.example.myapplication.tack_13;

import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_13.adapter.ArrangeAdapter;
import com.example.myapplication.tack_13.data.ArrageData;
import com.example.myapplication.tack_13.data.BannerData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;

import java.util.ArrayList;
import java.util.List;

public class OutpatientActivity extends AppCompatActivity {
    private ViewPager banner;
    private ImageView exit;
    private RecyclerView arrange;
    private SearchView shuosuo;
    private List<ArrageData.RowsDTO> arrList;
    private ArrageData arrageData;
    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_13_activity_outpatient);
        init();
        exit_();
        getBanner();
        getArrange();
        shuosuo_();

    }

    private void shuosuo_() {
        shuosuo.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Results(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                search(newText);
                return true;
            }
        });
    }

    private void search(String newText) {
        if(!"".equals(newText) && !TextUtils.isEmpty(newText)){
            arrList = new ArrayList<>();
            for (ArrageData.RowsDTO rowsDTO : arrageData.getRows()) {
                if(newText.trim().toLowerCase().contains(rowsDTO.getHospitalName())){
                    arrList.add(rowsDTO);
                }
            }
            ArrangeAdapter();
        }else{
            getArrange();
        }
    }

    private void Results(String query) {
        if(!"".equals(query) && !TextUtils.isEmpty(query)){
            arrList = new ArrayList<>();
            for (ArrageData.RowsDTO rowsDTO : arrageData.getRows()) {
                if(query.trim().toLowerCase().contains(rowsDTO.getHospitalName())){
                    arrList.add(rowsDTO);
                }
            }
            ArrangeAdapter();

        }else{
            getArrange();
        }
    }

    private void getArrange() {
        String url = "prod-api/api/hospital/hospital/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ArrageData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                arrageData = (ArrageData)obj;
                if(arrageData.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            arrList = arrageData.getRows();
                           ArrangeAdapter();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void ArrangeAdapter() {
        ArrangeAdapter arrangeAdapter = new ArrangeAdapter(arrList, OutpatientActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(OutpatientActivity.this);
        arrange.setLayoutManager(linearLayoutManager);
        arrange.setAdapter(arrangeAdapter);
    }

    private void getBanner() {
        String url = "prod-api/api/hospital/banner/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData bannerdata = (BannerData)obj;
                if(bannerdata.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayList<String> list = new ArrayList<>();
                            for (BannerData.DataDTO datum : bannerdata.getData()) {
                                list.add(datum.getImgUrl());
                            }
                            BannerTool bannerTool = new BannerTool(OutpatientActivity.this,list,banner);
                            banner.setAdapter(bannerTool);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OutpatientActivity.this.finish();
            }
        });
    }

    private void init() {
        banner = findViewById(R.id.banner);
        arrange = findViewById(R.id.arrange);
        exit = findViewById(R.id.exit);
        shuosuo = findViewById(R.id.shuosuo);

    }
}